package com.group3.merchant.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.group3.entity.Dish;
import com.group3.entity.DishType;
import com.group3.merchant.service.IDishService;
import com.group3.merchant.service.IDishTypeService;
import com.group3.merchant.service.impl.DishServiceImpl;
import com.group3.merchant.service.impl.DishTypeServiceImpl;
import com.group3.merchant.utils.SenderUtil;

/**
 * Servlet implementation class QueryDishServlet
 */
public class QueryDishServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IDishService service = new DishServiceImpl();
	private IDishTypeService dishTypeService = new DishTypeServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryDishServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		queryDish(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		queryDish(request, response);
	}
	private void queryDish(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
//		String pId = request.getParameter("pId");
//		String mId = request.getParameter("mId");
//		List<Dish> list = service.showByMTid(mId, pId);
//		PrintWriter writer = response.getWriter();
//		String jsonStr = JsonUtil.getInstance().ObjectToJson(list);
//		writer.write(jsonStr);
		String did = request.getParameter("dId");
		Dish dish = service.showByDid(did);
		List<DishType> dishtype = dishTypeService.showAllType();
		Map<String, Object> map = new HashMap<String, Object>();
		if(dish != null) {
			map.put("dish", dish);
			map.put("dishtype", dishtype);
			map.put("status", "200");
		} else {
			map.put("status", "401");
		}
		SenderUtil.send(map, response);
	}

}
